package com.weight.common.login.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.weight.common.VO.ResultMapVO;
import com.weight.common.enums.ResultEnum;
import com.weight.common.login.service.MenuInfoService;
import com.weight.common.login.service.RoleInfoService;
import com.weight.common.util.ChannelUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.UserContext;
import com.weight.system.command.PO.CommandInfo;
import com.weight.system.command.VO.CommandInfoVO;
import com.weight.system.command.service.CommandInfoService;
import com.weight.system.equmtgrp.PO.EqumtgrpInfo;
import com.weight.system.equmtgrp.service.EqumtgrpInfoService;
import com.weight.system.quick.PO.QuickInfo;
import com.weight.system.set.PO.SetInfo;
import com.weight.system.set.service.SetInfoService;
import com.weight.system.sysmenu.PO.SysMenuInfo;
import com.weight.system.sysmenu.service.SysMenuInfoService;
import com.weight.system.sysoper.PO.SysOperInfo;
import com.weight.system.sysoper.service.SysOperInfoService;
import com.weight.system.user.PO.UserInfo;
import com.weight.system.user.VO.UserInfoVO;
import com.weight.system.user.service.UserInfoService;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

 @RestController
 @Slf4j
//@Controller
public class LoginInfoFirstController {
	private static final Logger logger = LoggerFactory.getLogger(LoginInfoFirstController.class);
	 
	@Autowired
	MenuInfoService menuInfoService;
	@Autowired
	RoleInfoService roleInfoService; 	
	@Autowired
	private UserInfoService userInfoService;	
	@Autowired
	private SysOperInfoService sysOperInfoService;
	@Autowired
	private SetInfoService setInfoService;
	@Autowired
	private SysMenuInfoService sysMenuInfoService;	
	@Autowired
	private CommandInfoService commandInfoService;
	@Autowired
	private EqumtgrpInfoService equmtgrpInfoService;
	
	@RequestMapping("/login")
    public ModelAndView login(Model model,HttpServletRequest request){ 
//		 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("pages/login");
        return modelAndView;
	} 

	/**
     ** 登录
     * @param model
     * @param request
     * @return
     */
    @PostMapping("/loginInfo")
    public JsonResultUtil login(@RequestParam(value="username", required=true) String username,
                              @RequestParam(value="password", required=true) String password ) {
    	
    	//账号信息
    	SysOperInfo sysOperInfo = new SysOperInfo();
        try {
        	//1、 对输入的用户名和密码进行校验
        	Map<String,Object> inparam = new HashedMap<String,Object>();
        	 
        	inparam.put("operUname", username);
        	inparam.put("operPwd", password); 
			
			//查询账号信息表列表
			List<SysOperInfo> sysOperInfoList = sysOperInfoService.findSysOperInfoByT(inparam);
			log.info("sysOperInfoList={}",sysOperInfoList);
			
        	if(sysOperInfoList.size() == 0) {
//        		LOGIN_FAIL("25", "登录失败, 登录信息不正确"),
        		return new JsonResultUtil(ResultEnum.LOGIN_FAIL);
        	}
			
        	sysOperInfo = sysOperInfoList.get(0);
        	UserContext.setCurrebtSysOper(sysOperInfo);
        	 
        	
        	//用户ID
        	String userId = sysOperInfo.getUserId();        	
        	if(StringUtils.isBlank(userId)){
        		return new JsonResultUtil(ResultEnum.LOGIN_SUCC, "登陆成功！", "");
        	}
        	
        	//3.查询用户信息
        	inparam = new HashedMap<String,Object>();     	        	
        	inparam.put("userId", sysOperInfo.getUserId());
			log.info("查询人员信息表入参，map={}",inparam);		
			//用户信息
			UserInfo userInfo = userInfoService.findByUserInfoId(inparam);
			 
			if(null == userInfo) {
				return new JsonResultUtil(ResultEnum.LOGIN_SUCC, "登陆成功！", "");
			}
			UserInfoVO userInfoVO = new UserInfoVO();
    		BeanUtils.copyProperties(userInfo,userInfoVO);
    		UserContext.setCurrebtUser(userInfoVO);
    		 
        } catch (Exception e) {
            log.error("LoginInfoFirstController login 【校验失败】,username={},password={}",username,password);
            log.error("LoginInfoFirstController login Exception={}", e);
            return new JsonResultUtil(ResultEnum.SYS_ERROR);
        }
    	
    	return new JsonResultUtil(ResultEnum.LOGIN_SUCC, "登陆成功！", "");
    } 
    

    @RequestMapping("/logout")
    public ModelAndView logout(){
//        LOGOUT_SUCCESS("26", "登出成功"),
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("pages/login");  
        return modelAndView;
    }
    
    
    
     

    
    /**
     ** 首页
     * @param model
     * @param request
     * @return
     */
	@RequestMapping("/index")
    public ModelAndView index(){ 
//		 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("pages/index");  
        
        List<String> productList = new ArrayList<String>();
        productList.add("01.jpg");
        productList.add("02.jpg"); 
        modelAndView.addObject("productList", productList);
         
        
        List<QuickInfo> quickInfoList = new ArrayList<QuickInfo>(); 
        quickInfoList.add( new QuickInfo("实时称重","timeWeight"));
        quickInfoList.add( new QuickInfo("高级设置","weekly"));
        quickInfoList.add( new QuickInfo("称重信息","weightInfo/queryForWeightInfoList"));
        
        modelAndView.addObject("quickInfoList", quickInfoList);
//        proInfoList.add( new QuickInfo("",""));
//        proInfoList.add( new QuickInfo("",""));
//        proInfoList.add( new QuickInfo("",""));
//        List<String> proInfoList = new ArrayList<String>(); 
//        proInfoList.add("称重");
//        proInfoList.add("计量");
//        proInfoList.add("统计");        
//        modelAndView.addObject("proInfoList", proInfoList);
        
        
//        modelAndView.addObject("productTitle", "公司简介");
//        modelAndView.addObject("productDeatil", "孔子的远祖是宋国贵族，殷王室的后裔。周武王灭殷后，封殷宗室微子启于宋。由微子经微仲衍、宋公稽、丁公申，四传至泯公共。泯公长子弗父何让国于其弟鲋祀。弗父何为卿。孔子先祖遂由诸候家转为公卿之家。弗父何之曾孙正考父，连续辅佐宋戴公、武公、宣公，久为上卿，以谦恭著称于世。孔子六祖孔父嘉继任宋大司马。按周礼制，大夫不得祖诸候，“五世亲尽，别为公候”，故其后代以孔为氏。后宋太宰华父督作乱，弑宋殇公，杀孔父嘉。其后代避难奔鲁（孔氏为鲁国人自此始），卿位始失，下降为士。孔子曾祖父防叔曾任鲁防邑宰。祖父伯夏的事迹无考。父亲名纥，字叔，又称叔梁纥，为一名武士，以勇力著称。叔梁纥先娶施氏，无子，其妾生男，病足，复娶颜徵在，生孔子。");
        return modelAndView;
	}
	/**
     ** 消息中心
     * @param model
     * @param request
     * @return
     */
	@RequestMapping("/message")
    public ModelAndView message(Model model,HttpServletRequest request){ 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("pages/message");  
        return modelAndView;
	}
	
	/**
     ** 称重
     * @param model
     * @param request
     * @return
     */
	@RequestMapping("/weight")
    public ModelAndView weight(Model model,HttpServletRequest request){ 
		Map<Object, Object> map =new HashMap<>();
		map.put("menuStatus", 1); 
		
		//2、查询菜单信息表列表
		List<SysMenuInfo> sysMenuInfoList = sysMenuInfoService.findSysMenuInfoByT(map);
		log.info("sysMenuInfoList={}",sysMenuInfoList);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("sysMenuInfoList", sysMenuInfoList);
        modelAndView.setViewName("pages/weight");  
        return modelAndView;
	}
	/**
     ** 设置
     * @param model
     * @param request
     * @return
     */
	@RequestMapping("/setting")
    public ModelAndView setting(Model model,HttpServletRequest request){ 
//		 
		
		Map<Object, Object> map =new HashMap<>();
		map.put("setStatus", 1); 
		
		//2、查询设置管理表列表
		List<SetInfo> setInfoList = setInfoService.findSetInfoByT(map);
		log.info("setInfoList={}",setInfoList);
		//3、返回前台
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.addObject("setInfoVOList", setInfoList);
        modelAndView.setViewName("pages/setting");  
        return modelAndView;
	}
	
	@RequestMapping("/timeWeight")
    public ModelAndView timeWeight(){ 
		 ModelAndView modelAndView = new ModelAndView(); 
		 Map<Object, Object> map =new HashMap<>();
			map.put("equmtgrpStatus", 1);
//			SysOperInfo sysOperInfo =  UserContext.getCurreentSysOper();
//			
//			if(null == sysOperInfo) {
//				return modelAndView;
//			}
//			map.put("equmtgrpAuth1", sysOperInfo.getOperId());
//			map.put("equmtgrpAuth2", sysOperInfo.getOperId());
//			map.put("equmtgrpAuth3", sysOperInfo.getOperId());
			
			map.put("equmtgrpAuth1", "11"); 
			//2、查询设备管理表列表
			List<EqumtgrpInfo> equmtgrpInfoList = equmtgrpInfoService.findEqumtgrpInfoByT(map);
			
			//读量程指令
			String beginStr ="A55A020200";
			//发送指令
			//注册码
			String code = "";
			//连接串
			String address = "";
			//连接 会话
			Channel cx = null;
			System.out.println(ChannelUtil.userCodeMapTemp);
			System.out.println(ChannelUtil.userChannelMap);
			 
			EqumtgrpInfo equmtgrpInfo = null;
			for(int i=0;i<equmtgrpInfoList.size();i++) { 
				equmtgrpInfo = equmtgrpInfoList.get(i);
				code = equmtgrpInfo.getEqumtgrpIntcode();
				address = ChannelUtil.userCodeMapTemp.get(code);
				System.out.println("注册码：");
				System.out.println(code);
				System.out.println("连接串：");
				System.out.println(address);
				//连接串不存在 
				if(StringUtils.isBlank(address)) continue;
				
				cx = ChannelUtil.getChannel(address);
				//会话不存在
				if(null == cx) {
					System.out.println("会话不存在：" + address);
					continue;
				}
				 //发送 读量程指令
				cx.writeAndFlush(beginStr);
			}
			System.out.println(ChannelUtil.userChannelTemp);
			//读取 接收指令 临时
			Map<String, String> tempMap =  null;
			for(int i=0;i<equmtgrpInfoList.size();i++) { 
				equmtgrpInfo = equmtgrpInfoList.get(i);
				code = equmtgrpInfo.getEqumtgrpIntcode();
				address = ChannelUtil.userCodeMapTemp.get(code);
				//连接串不存在 
				if(StringUtils.isBlank(address)) continue;			
				tempMap = ChannelUtil.userChannelTemp.get(address); 
				
				//会话不存在
				if(null == tempMap) continue;
				//读量程有数据  状态 在线
				if(StringUtils.isNoneBlank((tempMap.get("0200")))) {
					equmtgrpInfo.setEqumtgrpOnline("1");
					equmtgrpInfo.setEqumtgrpAddress(address);
					equmtgrpInfo.setEqumtgrpDesc(tempMap.get("0200"));
				}
 
			}
			modelAndView.addObject("equmtgrpInfoList", equmtgrpInfoList);
	        modelAndView.setViewName("sysmenu/timeWeight");  
	        
	        return modelAndView;
		
	}
	/**
     ** 高级设置 
     *	当前用户可控设备
     * @param model
     * @param request
     * @return
     */
	@RequestMapping("/weekly")
    public ModelAndView weekly(){ 
//		 
        ModelAndView modelAndView = new ModelAndView(); 
//        //实时连接数
//        Iterator<String> ii = ChannelUtil.userChannelMap.keySet().iterator();
//        
//        List<String> equipmentList = new ArrayList<String>();
//        //过滤非本机构设备
//        String equipment = "";
//        while(ii.hasNext()) {
//        	equipment = ii.next();
//        	equipmentList.add(equipment);
//        }
        
//        System.out.println(equipmentList.size());
//        modelAndView.addObject("equipmentList", equipmentList);
 
//		modelAndView.setViewName("/equmtgrp/queryForEqumtgrpInfoList");
		
		Map<Object, Object> map =new HashMap<>();
		map.put("equmtgrpStatus", 1);
//		SysOperInfo sysOperInfo =  UserContext.getCurreentSysOper();
//		
//		if(null == sysOperInfo) {
//			return modelAndView;
//		}
//		map.put("equmtgrpAuth1", sysOperInfo.getOperId());
//		map.put("equmtgrpAuth2", sysOperInfo.getOperId());
//		map.put("equmtgrpAuth3", sysOperInfo.getOperId());
		
		map.put("equmtgrpAuth1", "11"); 
		//2、查询设备管理表列表
		List<EqumtgrpInfo> equmtgrpInfoList = equmtgrpInfoService.findEqumtgrpInfoByT(map);
		
		//读量程指令
		String beginStr ="A55A020200";
		//发送指令
		//注册码
		String code = "";
		//连接串
		String address = "";
		//连接 会话
		Channel cx = null;
		System.out.println(ChannelUtil.userCodeMapTemp);
		System.out.println(ChannelUtil.userChannelMap);
		 
		EqumtgrpInfo equmtgrpInfo = null;
		for(int i=0;i<equmtgrpInfoList.size();i++) { 
			equmtgrpInfo = equmtgrpInfoList.get(i);
			code = equmtgrpInfo.getEqumtgrpIntcode();
			address = ChannelUtil.userCodeMapTemp.get(code);
			System.out.println("注册码：");
			System.out.println(code);
			System.out.println("连接串：");
			System.out.println(address);
			//连接串不存在 
			if(StringUtils.isBlank(address)) continue;
			
			cx = ChannelUtil.getChannel(address);
			//会话不存在
			if(null == cx) {
				System.out.println("会话不存在：" + address);
				continue;
			}
			 //发送 读量程指令
			cx.writeAndFlush(beginStr);
		}
		
		//读取 接收指令 临时
		Map<String, String> tempMap =  null;
		for(int i=0;i<equmtgrpInfoList.size();i++) { 
			equmtgrpInfo = equmtgrpInfoList.get(i);
			code = equmtgrpInfo.getEqumtgrpIntcode();
			address = ChannelUtil.userCodeMapTemp.get(code);
			//连接串不存在 
			if(StringUtils.isBlank(address)) continue;			
			tempMap = ChannelUtil.userChannelTemp.get(address); 
			
			//会话不存在
			if(null == tempMap) continue;
			//读量程有数据  状态 在线
			if(StringUtils.isNoneBlank((tempMap.get("0200")))) {
				equmtgrpInfo.setEqumtgrpOnline("1");
				equmtgrpInfo.setEqumtgrpAddress(address);
				equmtgrpInfo.setEqumtgrpDesc(tempMap.get("0200"));
			} 
		}		 
		
		modelAndView.addObject("equmtgrpInfoList", equmtgrpInfoList);
        modelAndView.setViewName("sysmenu/weekly");  
        
        return modelAndView;
	}	
	
	/**
	 *	 高级设置  
	 * @param equipment
	 * @return
	 */
	@RequestMapping("/edit")
    public ModelAndView edit(String equipment){ 
//		 
		System.out.println("equipment:"+equipment);
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("sysmenu/edit"); 
        modelAndView.addObject("equipment", equipment);
        return modelAndView;
	}
	@RequestMapping("/timeWeighting")
    public ModelAndView timeWeighting(String equipment){ 
//		 
		System.out.println("equipment:"+equipment);
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("sysmenu/timeWeighting"); 
        modelAndView.addObject("equipment", equipment);
        return modelAndView;
	}
	
	/**
	 * 	高级设置  
	 *	 发送指令 获取当前设备参数实时信息
	 * 
	 * @param equipment
	 * @param commandGroup
	 * @return
	 */
	@RequestMapping("/setEquipmentInfo")
    public ModelAndView setEquipmentInfo(String equipment,String commandGroup){ 
		System.out.println("equipment:"+equipment+",commandGroup:"+commandGroup);
		
		Map<Object, Object> map =new HashMap<Object, Object>();
		map.put("status", 1);
		map.put("commandGroup", commandGroup);
		
		//2、查询指令表列表
		List<CommandInfo> commandInfoList =new ArrayList<CommandInfo>();
		if(StringUtils.equals("8", commandGroup) || StringUtils.equals("9", commandGroup)) {
			map.put("status", 1);
			map.put("commandGroup", commandGroup);
			commandInfoList = commandInfoService.findCommandInfoByT(map);
		}
		
		 
		//3、返回前台
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("sysmenu/setEquipmentInfo");
		modelAndView.addObject("commandInfoList",commandInfoList);		
		modelAndView.addObject("equipment",equipment);
		modelAndView.addObject("commandGroup",commandGroup);
        
        CommandInfo commandInfo = new CommandInfo();
        for(int i=0;i<commandInfoList.size();i++) {
        	commandInfo = commandInfoList.get(i);
        	if(2 == commandInfo.getAreaType()){
        		 getOptions(modelAndView, commandInfo) ;
        	}
        }
        
        Channel cx = ChannelUtil.getChannel(equipment);
		if(null == cx) {
		     return modelAndView;
		}
		
//		String beginStr ="A55A02"; 
//		//发送指令
//		for(int i=0;i<commandInfoList.size();i++) { 
//			 cx.writeAndFlush(beginStr + commandInfoList.get(i).getCommandMesg());
//		}
//		 
//		//动态 读取 接收指令
//		Map<String, String> userIdMap =  ChannelUtil.userChannelTemp.get(equipment);
//		String lenString = "";
//		for(int i=0;i<commandInfoList.size();i++) { 
//			lenString = checkMsg(userIdMap.get(commandInfoList.get(i).getCommandMesg()));
//			commandInfoList.get(i).setStandbyfield1(lenString);
//		}
	
		
		//发送指令
		if(StringUtils.equals("8", commandGroup)) {
//			redMsgA55A020200(commandInfoList, equipment) ;
			
			redMsgA55A020204(commandInfoList, equipment) ;
			 
		} else if(StringUtils.equals("9", commandGroup)) {
			redMsgA55A020220(commandInfoList, equipment) ;
			 modelAndView.setViewName("sysmenu/setEquipmentInfo");
		} else if(StringUtils.equals("7", commandGroup)) {
//			A55A020400 读0号内码  
			redMsgA55A020400(commandInfoList, equipment) ;
			modelAndView.addObject("titleName","传感器内码");
			modelAndView.addObject("commandid","0400");
			modelAndView.setViewName("sysmenu/setEquipmentInfoSE");
		} else if(StringUtils.equals("6", commandGroup)) {
			
//			读传感器系数 下发：A55A020306  A5 5A 02 03 06 
			redMsgA55A020306(commandInfoList, equipment) ;
			modelAndView.setViewName("sysmenu/setEquipmentInfo");
			
		} else if(StringUtils.equals("5", commandGroup)) {
			cx.writeAndFlush("");
		} else if(StringUtils.equals("4", commandGroup)) {
			cx.writeAndFlush("");
		}
	    
        
		System.out.println(commandInfoList);
        return modelAndView;
	}
	public void redMsgA55A020306(List<CommandInfo> commandInfoList, String equipment) {
		Channel cx = ChannelUtil.getChannel(equipment);
		cx.writeAndFlush("A55A020440");
		//动态 读取 接收指令
		Map<String, String> userIdMap =  ChannelUtil.userChannelTemp.get(equipment);		 
		System.out.println(111);
		System.out.println(userIdMap);
		String strStr = "";
		String command = "";
		for(int jj=0;jj<16;jj++) { 	
			command = "04"+ CommonUtils.checkSenfMessage(Integer.toString(40 + jj*4).toUpperCase());
			strStr = userIdMap.get(command);
			System.out.println("11" + jj);
			System.out.println("12" + strStr);
			if(StringUtils.isNotBlank(strStr)) {
				System.out.println("21" +jj);
				System.out.println("22" +command);
				commandInfoList.add(new CommandInfo(command,"传感器"+jj+"号",CommonUtils.hextToString(strStr)));
			}
			
		}
//		commandInfoList.add(new CommandInfo("0448","传感器8号","11111"));
	}
	public void redMsgA55A020400(List<CommandInfo> commandInfoList, String equipment) {
		 Channel cx = ChannelUtil.getChannel(equipment);
		 cx.writeAndFlush("A55A020400");
		//动态 读取 接收指令
		Map<String, String> userIdMap =  ChannelUtil.userChannelTemp.get(equipment);		 
		 System.out.println(222);
		 System.out.println(userIdMap);
		String strStr = "";
		String command = "";
		for(int i=0;i<16;i++) { 	
			command = "04"+ CommonUtils.checkSenfMessage(Integer.toString(i*4)).toUpperCase();
			
			System.out.println(command);
			strStr = userIdMap.get(command);
			System.out.println(strStr);
			if(StringUtils.isNotBlank(strStr)) {
				commandInfoList.add(new CommandInfo(command,"传感器"+i+"号",strStr));
			}
			
		}
//		commandInfoList.add(new CommandInfo("0408","传感器10号","1111"));
	}
	
	public void redMsgA55A020220(List<CommandInfo> commandInfoList, String equipment) {
		 Channel cx = ChannelUtil.getChannel(equipment);
		 cx.writeAndFlush("A55A020220");
		    try {
	            Thread.sleep(1000);
	        } catch (InterruptedException e) {
	            e.printStackTrace(); 
	        }
		//动态 读取 接收指令
		Map<String, String> userIdMap =  ChannelUtil.userChannelTemp.get(equipment);
		String lenString =  userIdMap.get("0220"); 
		if(StringUtils.length(lenString) < 28) {
			 System.out.println("长度不足");
			 return;
		}
		Map<String, String> map = new HashedMap<String, String>();
//		输入二段20000：A5 5A 08 02 20 30 32 30 30 30 30 
		map.put("0220", checkMsg(CommonUtils.asciiToStringHex(lenString.substring(0,12))));
//		输入二段分度值5：A5 5A 03 02 24 05
		map.put("0224", checkMsg(CommonUtils.hextToString(lenString.substring(12,14))));
//		输入三段40000：A5 5A 08 02 26 30 34 30 30 30 30 
		map.put("0226", checkMsg(CommonUtils.asciiToStringHex(lenString.substring(14,26))));
//		输入三段分度值：A5 5A 03 02 2A 05
		map.put("022A", checkMsg(CommonUtils.hextToString(lenString.substring(26,28)))); 
		
		for(int i=0;i<commandInfoList.size();i++) { 
			commandInfoList.get(i).setStandbyfield1(map.get(commandInfoList.get(i).getCommandMesg()));
		}
	}
	public void redMsgA55A020204(List<CommandInfo> commandInfoList, String equipment) {
		 Channel cx = ChannelUtil.getChannel(equipment);
		 cx.writeAndFlush("A55A020204");
		  
	        
		 
		 //动态 读取 接收指令
		 Map<String, String> userIdMap =  ChannelUtil.userChannelTemp.get(equipment);
		 String lenString =  userIdMap.get("0204");
		 if(StringUtils.length(lenString) < 18 ) {
			 System.out.println("长度不足");
			 return;
		 }
	 
		Map<String, String> map = new HashedMap<String, String>();
//		回传2：A5 5A 0b 02 04 01 02 02 01 05 05 03 01 01 
//		01 ：分度值(1,2,5,10,20,50,100) 0204
		map.put("0204", checkMsg(lenString.substring(0,2)));
//		02 ：小数点(0,1,2,3,4,5)0205
		map.put("0205", checkMsg(lenString.substring(2,4)));
//		02 ：滤波(0,1,2,3,4,5,6)0206
		map.put("0206", checkMsg(lenString.substring(4,6)));
//		01 ：0点跟踪(0,1,2,3,4,5,6)0207
		map.put("0207", checkMsg(lenString.substring(6,8)));
//		05 ：开机置0(显示：0%,2%,4%,20%,50%,100%,对应0,1,2,3,4,5)0208
		map.put("0208", checkMsg(lenString.substring(8,10)));
//		05 ：手动置0(显示：0%,2%,4%,20%,50%,100%,对应0,1,2,3,4,5)0209
		map.put("0209", checkMsg(lenString.substring(10,12)));
//		03 ：波特率(显示：600,1200,2400,4800,9600,对应0,1,2,3,4)020A
		map.put("020A", checkMsg(lenString.substring(12,14)));
//		01 ：协议(显示：3168,3190 A9+,电流环,对应0,1,2)020B
		map.put("020B", checkMsg(lenString.substring(14,16)));
//		01：三段分度值开关（显示：关，开,对应0,1）020C
		map.put("020C", checkMsg(lenString.substring(16,18)));
		System.out.println(map);
		for(int i=0;i<commandInfoList.size();i++) { 
			System.out.println(map.get(commandInfoList.get(i).getCommandMesg()));
			commandInfoList.get(i).setStandbyfield1(map.get(commandInfoList.get(i).getCommandMesg()));
		}
	}
	
	public void redMsgA55A020200(List<CommandInfo> commandInfoList, String equipment) {
		Channel cx = ChannelUtil.getChannel(equipment);
		cx.writeAndFlush("A55A020200");
		//动态 读取 接收指令
		for(int i=0;i<commandInfoList.size();i++) { 
			if(StringUtils.equals("0200", commandInfoList.get(i).getCommandMesg())) {
				commandInfoList.get(i).setStandbyfield1(
						checkMsg(
								ChannelUtil.userChannelTemp.get(equipment).get("0200")));
				break;
			}
			
		}
	}
	/**
	 * 	页面显示
	 * @param lenString
	 * @return
	 */
	public String checkMsg(String lenString) {
		if(StringUtils.isBlank(lenString) ) {			
			return "";		
		} 
		
		if(StringUtils.isBlank(lenString.replaceAll("^(0+)", ""))) {
			return "0";
		}
		lenString = lenString.replaceAll("^(0+)", ""); 
		return lenString;
	}
	
	
	/**
	 * 	拼接指令选择列表
	 * @param modelAndView
	 * @param commandInfo
	 */
	private void getOptions(ModelAndView modelAndView, CommandInfo commandInfo) {
		 String command= commandInfo.getCommandMesg();
		 String[] areas = commandInfo.getCommandArea().split(",");
		 String[] options =  commandInfo.getCommandArea().split(",");

		 if(StringUtils.isNotBlank(commandInfo.getAreaOption())) {
			 options = commandInfo.getAreaOption().split(",");
		 }
		 System.out.println();
		 List<ResultMapVO> mapList = new ArrayList<ResultMapVO>();
		 for(int i=0;i<areas.length;i++) {
			 mapList.add(new ResultMapVO(areas[i], options[i]));
			 
		 }
		 
	 
		 modelAndView.addObject("map"+command,mapList);
	}
	 

	/**
	 * 	指令设置
	 * @param commandInfoVO
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value="/sendEquipment")	 
	public JsonResultUtil sendEquipment(@Valid CommandInfoVO commandInfoVO,BindingResult bindingResult){
//		commandMesg="+id+"&commandId="+value+"&commandName="+ equipment;
		 String address = commandInfoVO.getCommandName();
		 Channel cx = ChannelUtil.getChannel(address);
		 if(null == cx) {
			 return new JsonResultUtil(ResultEnum.BAD_REQUEST, "设备不在线！", "");
		 }
		 
		 String commandMesg = commandInfoVO.getCommandMesg();
		 String sendMsg = commandInfoVO.getCommandId();
		 Integer commandType = commandInfoVO.getCommandType();
		 
		 sendMsg = CommonUtils.operSendMsg(commandMesg, sendMsg, commandType);
		 
		 
		 cx.writeAndFlush(sendMsg);
		 
//		 System.out.println(ChannelUtil.userChannelTemp.get(address).get(commandMesg));
//		 return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！",  checkMsg(ChannelUtil.userChannelTemp.get(address).get(commandMesg)));
		 return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", "");
	}

	
	/**
	 * 	指令设置 有返回值
	 * @param commandInfoVO
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value="/sendEquipmentResult")	 
	public JsonResultUtil sendEquipmentResult(CommandInfoVO commandInfoVO){
//		commandMesg="+id+"&commandId="+value+"&commandName="+ equipment;
		 String address = commandInfoVO.getCommandName();
		 String commandMesg = commandInfoVO.getCommandMesg();
		 String sendMsg = commandInfoVO.getCommandId();
		 Integer commandType = commandInfoVO.getCommandType();
		 
		
		 Channel cx = ChannelUtil.getChannel(address);
		 if(null == cx) {
			 return new JsonResultUtil(ResultEnum.BAD_REQUEST, "设备不在线！", "");
		 }
		 
		 sendMsg = CommonUtils.operSendMsg(commandMesg, sendMsg, commandType);
 
		 cx.writeAndFlush(sendMsg);
		 
		 System.out.println(ChannelUtil.userChannelTemp.get(address).get(commandMesg));
		 return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！",  checkMsg(ChannelUtil.userChannelTemp.get(address).get(commandMesg)));
	}
	
	@RequestMapping("/localHost")
    public ModelAndView localHost(){ 
		 ModelAndView modelAndView = new ModelAndView(); 
		 Map<Object, Object> map =new HashMap<>();
			map.put("equmtgrpStatus", 1);
//			SysOperInfo sysOperInfo =  UserContext.getCurreentSysOper();
//			
//			if(null == sysOperInfo) {
//				return modelAndView;
//			}
//			map.put("equmtgrpAuth1", sysOperInfo.getOperId());
//			map.put("equmtgrpAuth2", sysOperInfo.getOperId());
//			map.put("equmtgrpAuth3", sysOperInfo.getOperId());
			
			map.put("equmtgrpAuth1", "11"); 
			//2、查询设备管理表列表
			List<EqumtgrpInfo> equmtgrpInfoList = equmtgrpInfoService.findEqumtgrpInfoByT(map);
			
			//读量程指令
			String beginStr ="A55A020200";
			//发送指令
			//注册码
			String code = "";
			//连接串
			String address = "";
			//连接 会话
			Channel cx = null;
			System.out.println(ChannelUtil.userCodeMapTemp);
			System.out.println(ChannelUtil.userChannelMap);
			 
			EqumtgrpInfo equmtgrpInfo = null;
			for(int i=0;i<equmtgrpInfoList.size();i++) { 
				equmtgrpInfo = equmtgrpInfoList.get(i);
				code = equmtgrpInfo.getEqumtgrpIntcode();
				address = ChannelUtil.userCodeMapTemp.get(code);
				System.out.println("注册码：");
				System.out.println(code);
				System.out.println("连接串：");
				System.out.println(address);
				//连接串不存在 
				if(StringUtils.isBlank(address)) continue;
				
				cx = ChannelUtil.getChannel(address);
				//会话不存在
				if(null == cx) {
					System.out.println("会话不存在：" + address);
					continue;
				}
				 //发送 读量程指令
				cx.writeAndFlush(beginStr);
			}
			System.out.println(ChannelUtil.userChannelTemp);
			//读取 接收指令 临时
			Map<String, String> tempMap =  null;
			for(int i=0;i<equmtgrpInfoList.size();i++) { 
				equmtgrpInfo = equmtgrpInfoList.get(i);
				code = equmtgrpInfo.getEqumtgrpIntcode();
				address = ChannelUtil.userCodeMapTemp.get(code);
				//连接串不存在 
				if(StringUtils.isBlank(address)) continue;			
				tempMap = ChannelUtil.userChannelTemp.get(address); 
				
				//会话不存在
				if(null == tempMap) continue;
				//读量程有数据  状态 在线
				if(StringUtils.isNoneBlank((tempMap.get("0200")))) {
					equmtgrpInfo.setEqumtgrpOnline("1");
					equmtgrpInfo.setEqumtgrpAddress(address);
					equmtgrpInfo.setEqumtgrpDesc(tempMap.get("0200"));
				}
 
			}
			modelAndView.addObject("equmtgrpInfoList", equmtgrpInfoList);
	        modelAndView.setViewName("sysmenu/localHost");  
	        
	        return modelAndView;
		
	}
	 
	/**
     ** 百度地图
     * @param model
     * @param request
     * @return
     */
	@RequestMapping("/baidu")
    public ModelAndView baidu(String equipment){  
//		 System.out.println("baidubaidubaidubaidubaidubaidu1");
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.setViewName("sysmenu/baidu");  
        modelAndView.addObject("equipment", equipment);
        
        Channel cx = ChannelUtil.getChannel(equipment);
		if(null == cx) {
		     return modelAndView;
		}
		String beginStr = "404454553A303030303A475053494E464F0D";
		//动态 读取 接收指令
		Map<String, String> map =  ChannelUtil.userChannelTemp.get(equipment);
//		map.put("GPSINFO", "");		
		//GPS
		cx.writeAndFlush(beginStr);
		String mapStr = map.get("GPSINFO");	
		System.out.println(mapStr);
		if(StringUtils.contains(mapStr, "+GPSINFO:")) {
			mapStr = mapStr.replaceAll("OK", "").replaceAll("\\+GPSINFO:","").replaceAll("\r|\n|\\s", "");
			System.out.println(mapStr);
			String[] mapStrs = mapStr.split(",");
			//036.5820617,116.9338073
			modelAndView.addObject("hostX",  checkMsg(mapStrs[0]));//36.5820617
			modelAndView.addObject("hostY",  checkMsg(mapStrs[1]));//116.9338073
			modelAndView.addObject("hostStrs",  "[{title:\"阿凡达\",content:\"分度值：5000\",point:\""
					+ checkMsg(mapStrs[1]) + "|"
							+ checkMsg(mapStrs[0]) + "\",isOpen:0,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}}]");//116.9338073
			 
			System.out.println("成功");
		}
		System.out.println("baidubaidubaidubaidubaidubaidu2");
//		+GPSINFO: 036.5820617,116.9338073
//2B47505349
//		OK
        return modelAndView;
	}
	

    @RequestMapping("/pages/403")
    public String unauthorizedRole(){
        logger.info("------没有权限-------");
        return "pages/403";
    }
	
	
	
	
	
}
